import { getJsRandom, jsSleep } from '@renderer/system/utils/commonUtils';

function getDefaultObj(winName, frameId, frameUrl) {
    return {
        winName: winName,
        frameId: frameId || '',
        frameUrl: frameUrl || '',
        htmlTag: '', // 目标对象
        action: '', // 操作类型 goto type  click  imgSrc getAttr downImg
        ajaxExp: 2000, // 超时时间 单位毫秒
        attr: '', // html 属性值
        value: '', // html值
        urlStr: '', // url地址
        waitUrl: '', // 等待url
        imgSrc: '', // 图片路径
        timeout: '', // 超时时间
        key: '', // 操作对象的key值
        filePath: '', // 上传文件路径
        execJsStr: '', // 执行的js脚本
        eleSelctType: 'selector', // 元素选择器，默认 selector
    };
}
export const ELE_SELCT_TYPE_XPATH = 'xpath';
let triggerClickPathAndNewPageCount = 0;
export class OpWebDom {
    // 操作的窗口名称
    constructor(winName, frameId = '', frameUrl = '') {
        this.winName = winName;
        this.frameId = frameId || '';
        this.frameUrl = frameUrl || '';
    }

    getDefaultParam() {
        return getDefaultObj(this.winName, this.frameId, this.frameUrl);
    }

    // 新建立一个窗口
    async newWindow(options = {}) {
        let optionsTmp = Object.assign(
            {
                show: true, // 是否显示
                openDevTools: false, // 是否打开开发者工具
                alwaysNew: false, // 是否总是开启一个新窗口，true 如果存在老窗口就会关闭,再从新打开，false 老窗口存在则继续用老窗口
            },
            options,
        );
        await window.pieClient.newWindow(this.winName, optionsTmp);
    }

    async newWindowByTitle(title, options = {}) {
        let optionsTmp = Object.assign(
            {
                show: true, // 是否显示
                openDevTools: false, // 是否打开开发者工具
                alwaysNew: false, // 是否总是开启一个新窗口，true 如果存在老窗口就会关闭,再从新打开，false 老窗口存在则继续用老窗口
            },
            options,
        );
        await window.pieClient.newWindowByTitle(title, optionsTmp);
    }

    // 访问URL
    async goto(url) {
        let dx = this.getDefaultParam();
        dx.action = 'goto';
        dx.urlStr = url;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 窗口截图并保存
    async winCaptureScreen(url, imgUrl) {
        let dx = this.getDefaultParam();
        dx.action = `winCaptureScreen`;
        dx.imgExpUrl = imgUrl;
        return await window.pieClient.opHtmlDom(dx);
    }

    async execJs(execJsStr) {
        let dx = this.getDefaultParam();
        dx.action = 'execJs';
        dx.execJsStr = execJsStr;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 给元素输入值，如input等
    async setInputVal(htmlTag, value) {
        let dx = this.getDefaultParam();
        dx.action = 'setInputVal';
        dx.htmlTag = htmlTag;
        dx.value = value;
        return await window.pieClient.opHtmlDom(dx);
        // return dx;
    }
    // 给元素输入值，如input等
    async setInputValByXpath(htmlTag, value) {
        let dx = this.getDefaultParam();
        dx.action = 'setInputValByXpath';
        dx.htmlTag = htmlTag;
        dx.value = value;
        dx.eleSelctType = ELE_SELCT_TYPE_XPATH;
        return await window.pieClient.opHtmlDom(dx);
        // return dx;
    }
    // 给select元素赋值
    async setSelectVal(htmlTag, value) {
        let dx = this.getDefaultParam();
        dx.action = 'setSelectVal';
        dx.htmlTag = htmlTag;
        dx.value = value;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 单机某个 元素
    async triggerClick(htmlTag, eleSelctType) {
        let dx = this.getDefaultParam();
        dx.action = 'triggerClick';
        dx.htmlTag = htmlTag;
        if (eleSelctType) {
            dx.eleSelctType = eleSelctType;
        }
        return await window.pieClient.opHtmlDom(dx);
    }

    // 单机某个 元素
    async triggerClickByXpath(htmlTag) {
        let dx = this.getDefaultParam();
        dx.action = 'triggerClick';
        dx.htmlTag = htmlTag;
        dx.eleSelctType = ELE_SELCT_TYPE_XPATH;
        return await window.pieClient.opHtmlDom(dx);
    }

    async htmlPositionByXpath(htmlTag) {
        let dx = this.getDefaultParam();
        dx.action = 'htmlPositionByXpath';
        dx.htmlTag = htmlTag;
        dx.eleSelctType = ELE_SELCT_TYPE_XPATH;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 获取元素属性值
    async getAttrVal(htmlTag, attr) {
        let dx = this.getDefaultParam();
        dx.action = 'getAttrVal';
        dx.htmlTag = htmlTag;
        dx.attr = attr;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 获取page页面的 LocalStorage
    async getLocalStorageByKey(key) {
        let dx = this.getDefaultParam();
        dx.action = 'getLocalStorageByKey';
        dx.key = key;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 给LocalStorage添加key-value
    async setLocalStorageKeyVal(key, val) {
        let dx = this.getDefaultParam();
        dx.action = 'setLocalStorageKeyVal';
        dx.key = key;
        dx.value = val;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 给SessionStorage 添加key-value
    async setSessionStorageKeyVal(key, val) {
        let dx = this.getDefaultParam();
        dx.action = 'setSessionStorageKeyVal';
        dx.key = key;
        dx.value = val;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 清除浏览器缓存
    async reload() {
        let dx = this.getDefaultParam();
        dx.action = 'reload';
        return await window.pieClient.opHtmlDom(dx);
    }

    // 清除LocalStorage中的值
    async clearLocalStorageByKey(key) {
        let dx = this.getDefaultParam();
        dx.action = 'clearLocalStorageByKey';
        dx.key = key;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 清SessionStorage中的值
    async clearSessionStorageByKey(key) {
        let dx = this.getDefaultParam();
        dx.action = 'clearSessionStorageByKey';
        dx.key = key;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 清SessionStorage中的值
    async clearCookies(key) {
        let dx = this.getDefaultParam();
        dx.action = 'clearCookies';
        dx.key = key;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 清SessionStorage中的值
    async ztcPageRequestMonitor() {
        let dx = this.getDefaultParam();
        dx.action = 'ztcPageRequestMonitor';
        return await window.pieClient.opHtmlDom(dx);
    }

    // 等待页面中加载目标html
    async waitForSelector(htmlTag, timeout = 5000) {
        let dx = this.getDefaultParam();
        dx.action = 'waitForSelector';
        dx.htmlTag = htmlTag;
        dx.timeout = timeout;
        return await window.pieClient.opHtmlDom(dx);
    }
    // 等待页面中加载目标html
    async waitForXPath(htmlTag, timeout = 5000) {
        let dx = this.getDefaultParam();
        dx.action = 'waitForXPath';
        dx.htmlTag = htmlTag;
        dx.timeout = timeout;
        return await window.pieClient.opHtmlDom(dx);
    }
    async waitForNetworkIdle(timeout = 5000) {
        let dx = this.getDefaultParam();
        dx.action = 'waitForNetworkIdle';
        dx.timeout = timeout;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 上传文件
    async uploadFile(htmlTag, filePath) {
        let dx = this.getDefaultParam();
        dx.action = 'uploadFile';
        dx.htmlTag = htmlTag;
        dx.filePath = filePath;
        return await window.pieClient.opHtmlDom(dx);
    }

    // 给主页面添加监控
    async monitorPage() {
        let dx = this.getDefaultParam();
        dx.action = 'monitorPage';
        return await window.pieClient.opHtmlDom(dx);
    }

    // 给iframe页面添加监控
    async monitorFrame(frameId, frameUrl) {
        let dx = this.getDefaultParam();
        dx.frameId = frameId;
        dx.frameUrl = frameUrl;
        dx.action = 'monitorFrame';
        return await window.pieClient.opHtmlDom(dx);
    }

    // todo 开始操作frame，设置frameid或url
    async startOperaFrame(frameId, frameUrl) {
        this.frameId = frameId;
        this.frameUrl = frameUrl;
    }

    // todo 通过条件找寻frame
    async findFrameByCond(frameId, frameUrl) {
        let dx = this.getDefaultParam();
        dx.frameId = frameId;
        dx.frameUrl = frameUrl;
        dx.action = 'findFrameByCond';
        return await window.pieClient.opHtmlDom(dx);
    }

    /************************ 王慧君新方法 **************/

    async triggerClickPathAndNewPage(htmlTag, newWinName) {
        let dx = this.getDefaultParam();
        dx.action = 'triggerClickPathAndNewPage';
        dx.htmlTag = htmlTag;
        dx.eleSelctType = ELE_SELCT_TYPE_XPATH;
        dx.newWinName = newWinName;
        // 制作方法同一时间只能访问一次

        try {
            if (triggerClickPathAndNewPageCount > 0) {
                await jsSleep(1000);
            }
            triggerClickPathAndNewPageCount++;
            return await window.pieClient.opHtmlDom(dx);
        } finally {
            triggerClickPathAndNewPageCount--;
            if (triggerClickPathAndNewPageCount < 0) {
                triggerClickPathAndNewPageCount = 0;
            }
        }
    }

    // 拖拽
    async draggableHtml(htmlTag, htmlTag1) {
        let dx = this.getDefaultParam();
        dx.action = 'draggableHtml';
        dx.htmlTag = htmlTag;
        dx.htmlTag1 = htmlTag1;
        dx.eleSelctType = ELE_SELCT_TYPE_XPATH;
        return await window.pieClient.opHtmlDom(dx);
    }
}
